import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import '../core/network_interceptor.dart';
import '../core/network_request.dart';

class NetworkLogger extends NetworkInterceptor {
  @override
  int get priority => 1;

  @override
  void willSend(RequestOptions options, RequestInterceptorHandler handler,
      TargetType target) {
    final description = target.path.description;
    debugPrint('''
🔫🔫🔫  Request ║ ${options.method} 🔫🔫🔫
$description ${options.uri.toString()}

__________ Parameters

${options.data ?? options.queryParameters}

__________ Headers

${options.headers}''');
    super.willSend(options, handler, target);
  }

  @override
  void didReceive(Response response, ResponseInterceptorHandler handler,
      TargetType target) {
    final description = target.path.description;
    final options = response.requestOptions;
    debugPrint('''
📥📥📥 Response ║ ${options.method} ║ Status: ${response.statusCode} ${response.statusMessage} 📥📥📥
$description ${options.uri.toString()}

__________ Parameters

${options.data ?? options.queryParameters}

__________ Headers

${options.headers}

__________ Body

${response.data.toString()}''');
    super.didReceive(response, handler, target);
  }

  @override
  void didError(
      DioException err, ErrorInterceptorHandler handler, TargetType target) {
    if (err.type == DioExceptionType.badResponse) {
      final options = err.requestOptions;
      final response = err.response;
      final description = target.path.description;
      debugPrint('''
Error ║ ${options.method} ║ Status: ${response?.statusCode} ${response?.statusMessage}
$description ${options.uri.toString()}
Parameters: ${options.data ?? options.queryParameters}
Headers: ${options.headers}
Body: ${response?.data.toString()}
''');
    }
    super.didError(err, handler, target);
  }
}
